﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Telerik.Windows.Controls.GridView;
using Baosight.ZNH.ZMCM.UI.ChildWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls;
using Baosight.Platform.Core.ServiceModel;
using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCM.UI.Utils;

namespace Baosight.ZNH.ZMCM.UI
{
    public partial class ZMCMK1 : EFViewCmes
    {
        public ZMCMK1()
        {
            InitializeComponent();
            this.BeforeAction += OnBeforeAction;
            this.LayoutUpdated += new EventHandler(ZMCMK1_LayoutUpdated);
        }

        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);
           
        }

        /// <summary>
        /// 修改前设置属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 根据主表的状态设置按钮是否可见
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ZMCMK1_LayoutUpdated(object sender, EventArgs e)
        {
            if (this.treeTypeLevel.Value == null)
            {
                return;
            }
            if (this.treeTypeLevel.Value.ToString().Equals("0")
                || this.treeTypeLevel.Value.ToString().Equals("1"))
            {
                Button insertbut = this.FindName("SELECTMATERIAL") as Button;
                if (insertbut != null)
                    insertbut.IsEnabled = false;
                Button deletebut = this.FindName("DELETE") as Button;
                if (deletebut != null)
                    deletebut.IsEnabled = false;
            }
        }

        /// <summary>
        /// 绑定值到指定的Block内 
        /// </summary>
        /// <param name="block"></param>
        /// <param name="columnName"></param>
        /// <param name="dataValue"></param>
        private void BindToBlock(EIBlock block, string columnName, object dataValue, EIRow row)
        {
            EIColumn column = new EIColumn();
            column.SetName(columnName);
            row.Add(columnName, dataValue);
            block.AddColumn(column);
        }

        /// <summary>
        /// “选择隐患”按钮执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnSELECTMATERIALButtonClick(object sender, RoutedEventArgs e)
        {
            Button but = sender as Button;
            but.IsEnabled = false;
            TempButton = but;

            EIInfo eiInfo = new EIInfo();
            EIBlock inquBlock = new EIBlock(EIConstants.QueryStatusBlock);
            EIRow row = new EIRow();
            BindToBlock(inquBlock, "fdMaterialName", "", row);
            BindToBlock(inquBlock, "fdMaterialCode", "", row);
            BindToBlock(inquBlock, "fdTypeCode", "", row);
            inquBlock.AddRow(row);
            eiInfo.AddBlock(inquBlock);
            HttpServiceAgent agent = new HttpServiceAgent();
            eiInfo.Attributes.Add(EIConstants.ServiceNameKey, "ZMCMK1");
            eiInfo.Attributes.Add(EIConstants.MethodNameKey, "queryMaterial");
            agent.CallService(eiInfo, OnDoSelectCompleted);
        }

        /// <summary>
        /// 选择隐患的回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoSelectCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status == -1)
            {
                EFWindow.Alert(e.EIInfo.GetMsg());
                return;
            }

            if (e.EIInfo.GetBlock(EIConstants.ResultBlock).GetRowCount() == 0)
            {
                base.OpenEFWindowAlert("没有可供选择的物资！", true);
                return;
            }
            SelectMaterial SelectMaterial = new SelectMaterial();
            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.Header = "选择物资记录";
            _popup.ResizeMode = ResizeMode.NoResize;
            _popup.WindowStartupLocation = Telerik.Windows.Controls.WindowStartupLocation.CenterScreen;
            SelectMaterial.EIDataContext = new Platform.Core.UI.EIDataContext();
            SelectMaterial.EIDataContext.Update(e.EIInfo);
            SelectMaterial.fdMaterialTypeId.Value = this.treeTypeId.Value;
            SelectMaterial.fdMaterialType.Value = this.treeTypeName.Value;
            SelectMaterial.fdMaterialInnerCode.Value = this.treeTypeInnerCode.Value;
            _popup.Content = SelectMaterial;
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(Window_Closed);
            _popup.ShowDialog();
            _popup.DialogResult = false;
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.grid.Refresh();
            }
            //按钮生效
            base.TempButtonOpen(true);
            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }
    }
}
